<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form :form="form" slot="detail">
        <a-row>
          <a-col :span="24">
            <a-form-item label="团员" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <!-- <a-input v-decorator="['userId', validatorRules.userId]" placeholder="请输入团员编号"></a-input> -->
              <!-- <XSearchUser :disabled="userDisabled" v-decorator="['userId']" :showInfo="false" @change="setUserInfo"></XSearchUser> -->
              <x-select-user-by-dep :disabled="userDisabled" v-decorator="['userId', validatorRules.userId]" :multi="false"></x-select-user-by-dep>
            </a-form-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-item label="姓名" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input disabled v-decorator="['realname', validatorRules.realname]" placeholder="请输入姓名"></a-input>
            </a-form-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-item label="政治面貌" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="radio" v-decorator="['politicalStatus', validatorRules.politicalStatus]" :trigger-change="true" dictCode="political_outlook" placeholder="请选择政治面貌"/>
            </a-form-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-item label="职务" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag v-decorator="['position']" :trigger-change="true" dictCode="league_position" placeholder="请选择职务"/>
            </a-form-item>
          </a-col> -->
          <a-col :span="24">
            <a-form-item label="毕业状态" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-dict-select-tag type="radio" v-decorator="['graduateStatus', validatorRules.graduateStatus]" :trigger-change="true" dictCode="graduate" placeholder="请选择毕业状态"/>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="入团时间" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-date placeholder="请选择入团时间" v-decorator="['joinTime', validatorRules.joinTime]" :trigger-change="true" style="width: 100%"/>
            </a-form-item>
          </a-col>
          <!-- <a-col :span="24">
            <a-form-item label="学院" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input disabled v-decorator="['college']" placeholder="请输入学院"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="专业" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input disabled v-decorator="['major']" placeholder="请输入专业"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="年级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input disabled v-decorator="['grade']" placeholder="请输入年级"></a-input>
            </a-form-item>
          </a-col>
          <a-col :span="24">
            <a-form-item label="班级" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <a-input disabled v-decorator="['clazz']" placeholder="请输入班级"></a-input>
            </a-form-item>
          </a-col> -->
          <!-- <a-col :span="24">
            <a-form-item label="归属部门" :labelCol="labelCol" :wrapperCol="wrapperCol">
              <j-select-depart v-decorator="['orgId', validatorRules.orgId]" />
            </a-form-item>
          </a-col> -->
          <a-col v-if="showFlowSubmitButton" :span="24" style="text-align: center">
            <a-button @click="submitForm">提 交</a-button>
          </a-col>
        </a-row>
      </a-form>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import pick from 'lodash.pick'
  import { validateDuplicateValue } from '@/utils/util'
  import JFormContainer from '@/components/jeecg/JFormContainer'
  import JDate from '@/components/jeecg/JDate'  
  import JDictSelectTag from "@/components/dict/JDictSelectTag"
  import JSelectDepart from '@/components/jeecgbiz/JSelectDepart'
  import XSearchUser from '@/components/extends/XSearchUser'
  import XSelectUserByDep from '@/components/extends/XSelectUserByDep'
  export default {
    name: 'LeagueMemForm',
    components: {
      JFormContainer,
      JDate,
      JDictSelectTag,
      JSelectDepart,
      XSearchUser,
      XSelectUserByDep
    },
    props: {
      //流程表单data
      formData: {
        type: Object,
        default: ()=>{},
        required: false
      },
      //表单模式：true流程表单 false普通表单
      formBpm: {
        type: Boolean,
        default: false,
        required: false
      },
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      }
    },
    data () {
      return {
        userDisabled:false,
        form: this.$form.createForm(this),
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        validatorRules: {
          userId: {
            rules: [
              { required: true, message: '请输入学号!'},
            ]
          },
          realname: {
            rules: [
              { required: true, message: '请输入姓名!'},
            ]
          },
          politicalStatus: {
            rules: [
              { required: true, message: '请输入政治面貌!'},
            ]
          },
          // position: {
          //   rules: [
          //     { required: true, message: '请输入职务!'},
          //   ]
          // },
          graduateStatus: {
            rules: [
              { required: true, message: '请输入毕业状态!'},
            ]
          },
          joinTime: {
            rules: [
              { required: true, message: '请输入入团时间!'},
            ]
          },
          // orgId: {
          //   rules: [
          //     { required: true, message: '请选择部门!'},
          //   ]
          // },
        },
        url: {
          add: "/leagueMem/add",
          edit: "/leagueMem/edit",
          queryById: "/leagueMem/queryById"
        }
      }
    },
    computed: {
      formDisabled(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return false
          }
          return true
        }
        return this.disabled
      },
      showFlowSubmitButton(){
        if(this.formBpm===true){
          if(this.formData.disabled===false){
            return true
          }
        }
        return false
      }
    },
    created () {
      //如果是流程中表单，则需要加载流程表单data
      this.showFlowData();
    },
    methods: {
      add () {
        this.userDisabled = false;
        this.open({});
      },
      edit(record){
        this.userDisabled = true;
        this.open(record);
      },
      open (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'userId','politicalStatus','position','graduateStatus','joinTime'))
        })
      },
      setUserInfo(data){
        data.realname = data.name
        delete data.name
        this.model = Object.assign({},this.model,data)
        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,'userId','politicalStatus','position','graduateStatus','joinTime'))
        })
      },
      //渲染流程表单数据
      showFlowData(){
        if(this.formBpm === true){
          let params = {id:this.formData.dataId};
          getAction(this.url.queryById,params).then((res)=>{
            if(res.success){
              this.edit (res.result);
            }
          });
        }
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            let formData = Object.assign(this.model, values);
            
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
      popupCallback(row){
        this.form.setFieldsValue(pick(row,'userId','realname','politicalStatus','position','punishes','graduateStatus','joinTime','college','major','grade','clazz'))
      },
    }
  }
</script>